package com.meirimoney.app;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.meirimoney.app.control.HeadControl;
import com.meirimoney.app.control.MenuBar;
import com.meirimoney.sdk.models.Commodity;

public class AlipayWithdraw extends BaseExchange
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.mrm_alipay_withdraw);
		this.setTitle();
		this.initializeUI(this.commodity);
	}

	/***
	 * 初始化当前界面的UI
	 */
	private void initializeUI(Commodity commodity)
	{
		if (null == this.hintTab)
		{
			this.hintTab = (TextView) this.findViewById(R.id.hintTab);
			String format = this.getString(R.string.alipayWithdrawHintTab);
			this.hintTab.setText(String.format(format,
					String.valueOf(commodity.Price)));
		}
		if (null == this.alipayAccount)
		{
			this.alipayAccount = (EditText) this
					.findViewById(R.id.alipayAccount);
		}
		if (null == this.confirmAlipayAccount)
		{
			this.confirmAlipayAccount = (EditText) this
					.findViewById(R.id.confirmAlipayAccount);
		}
		if (null == this.reallyName)
		{
			this.reallyName = (EditText) this.findViewById(R.id.reallyName);
		}
		if (null == this.submitButton)
		{
			this.submitButton = (Button) this.findViewById(R.id.submit);
			this.submitButton.setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v)
	{
		if (this.checkBalance())
		{
			super.onClick(v);
			String alipayAccount = this.alipayAccount.getText().toString();
			String confirmAlipayAccount = this.confirmAlipayAccount.getText()
					.toString();
			String reallyName = this.reallyName.getText().toString();
			if (this.checkForm(alipayAccount, confirmAlipayAccount, reallyName))
			{
				String shipAddress = alipayAccount + "," + reallyName;
				this.submit(1, shipAddress);
			}
		}
	}

	/**
	 * 检测当前表单输入的合法性
	 * 
	 * @return True：正确，False：不正确
	 */
	private boolean checkForm(String alipayAccount,
			String confirmAlipayAccount, String reallyName)
	{
		if (alipayAccount.equals(""))
		{
			this.showToastMessage("请输入支付宝账号");
		} else if (confirmAlipayAccount.equals(""))
		{
			this.showToastMessage("请再次输入支付宝账号");
		} else if (!alipayAccount.equals(confirmAlipayAccount))
		{
			this.showToastMessage("两次输入的支付宝账号不一致");
		} else if (reallyName.equals(""))
		{
			this.showToastMessage("请输入真实姓名");
		} else
		{
			return true;
		}
		return false;
	}

	/**
	 * 支付宝账号信息
	 */
	private EditText confirmAlipayAccount;

	/**
	 * 支付宝账号信息
	 */
	private EditText alipayAccount;

	/**
	 * 真是姓名
	 */
	private EditText reallyName;
}
